package cc.vicp.djx314.starfood.util.time;

import java.beans.PropertyEditorSupport;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 字符串和时间的转换类
 * @author 水山清风
 *
 */
public class DateEditor extends PropertyEditorSupport {
	public static final Logger logger = LoggerFactory.getLogger(DateEditor.class);

	@Override
	public void setAsText(String text) throws IllegalArgumentException {
		try {
			Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(text);
			setValue(date);
		} catch (ParseException e) {
			try {
				Date date = new SimpleDateFormat("yyyy-MM-dd").parse(text);
				setValue(date);
			} catch (ParseException e1) {
				logger.error("时间转换错误", e);
				throw new IllegalArgumentException(e);
			}
		}
	}
}
